package com.hhkj.admin.order.service.extend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.admin.common.cont.NumTypeEnum;
import com.ruoyi.common.utils.num.NumUtils;
import com.hhkj.admin.order.common.cont.OrderRefundConst;
import com.hhkj.admin.order.domain.OrderRefund;
import com.hhkj.admin.order.domain.OrderRefundItemAsso;
import com.hhkj.admin.order.model.dopo.OrderRefundDo;
import com.hhkj.admin.order.model.dopo.OrderRefundPo;
import com.hhkj.admin.order.service.base.OrderRefundItemAssoService;
import com.hhkj.admin.order.service.base.OrderRefundService;
import com.hhkj.admin.order.service.extend.OrderExtendService;
import com.hhkj.admin.order.service.extend.OrderRefundExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderRefundExtendServiceImpl
        extends BaseExtendServiceImpl<OrderRefund, OrderRefundDo, OrderRefundPo, OrderRefundService>
        implements OrderRefundExtendService {

    @Autowired
    private OrderExtendService orderExtendService;
    @Autowired
    private OrderRefundItemAssoService refundItemAssoService;

    @Override
    public OrderRefund getByNum(String num) {
        LambdaQueryWrapper<OrderRefund> refundQuery = new LambdaQueryWrapper<>();
        refundQuery.eq(OrderRefund::getNum, num)
                .orderByDesc(OrderRefund::getId);
        return this.getService().getOne(refundQuery, false);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(OrderRefundDo refundDo) {
        // 1、新增退款信息
        refundDo.setStatusCode(OrderRefundConst.StatusEnum.REFUNDING.getCode());
        refundDo.setStartTime(new Date());
        refundDo.setStudentAmount(refundDo.getItemIdList().size());
        //获取今日退款数量，用于生成退款单号
        Long todayRefundAmount = getService().countByToday();
        String num = NumUtils.generateNum(NumTypeEnum.REFUND.getPrefix(), ++todayRefundAmount);
        refundDo.setNum(num);
        boolean result = this.getService().save(refundDo);
        if (!result) {
            return false;
        }
        // 2、保存itemAsso信息
        List<OrderRefundItemAsso> refundItemAssoDoList = refundDo.getItemIdList().stream()
                .map(itemId -> new OrderRefundItemAsso(itemId, refundDo.getId()))
                .collect(Collectors.toList());
        refundItemAssoService.saveBatch(refundItemAssoDoList);
        //
        orderExtendService.applyRefund(refundDo.getOrderId(), refundDo.getItemIdList());
        return true;
    }

    @Override
    public void cancelById(Long id) {
        LambdaUpdateWrapper<OrderRefund> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrderRefund::getId, id)
                .set(OrderRefund::getStatusCode, OrderRefundConst.StatusEnum.CANCEL.getCode());
        this.getService().update(updateWrapper);
    }

    @Override
    public OrderRefund getRefundingByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderRefund> refundQuery = new LambdaQueryWrapper<>();
        refundQuery.eq(OrderRefund::getOrderId, orderId)
                .eq(OrderRefund::getStatusCode, OrderRefundConst.StatusEnum.REFUNDING.getCode())
                .orderByDesc(OrderRefund::getId);
        return this.getService().getOne(refundQuery, false);
    }
}
